package middol.utils

/**
* @Description:    实体类操作工具类
* @Author:         zhangjiayu@middol.com
* @CreateDate:     2020/10/28 17:58
* @Version:        1.0
*/
class DomainUtils {
    /**
     * 实体类属性白名单 剔除属性
     */
    private static final List DOMAIN_WHITE_LIST = ["logCurrentUserName", 'logIncluded', 'logAssociatedIds', 'logIgnoreEvents', 'auditableDirtyPropertyNames', 'logExcluded', 'logClassName', 'logEntityId', 'logURI', 'auditablePropertyNames', 'logVerboseEvents', 'logMaskProperties']

    private static final String DOMAIN_WHITE_SERVICE = "Service"
    /**
     * 实体类转Map
     * @param domain
     * @return
     */
    static domainToMap(Object domain){
        if(!domain){
            return null
        }
        def result = [id: domain?.id]
        domain.properties.each {String k,v ->
            if(!DOMAIN_WHITE_LIST.contains(k) && !k.contains(DOMAIN_WHITE_SERVICE)){
                result[k] = v
            }
        }
        result
    }

    /**
     * 实体类列表转Map列表
     * @param domainList
     * @return
     */
    static domainListToMapList(List domainList){
        def result = []
        domainList.each {Object domain ->
            result << domainToMap(domain)
        }
        result
    }
}
